package com.zywl.web.controller.sys;

import com.zywl.core.beans.User;
import com.zywl.service.RoleService;
import com.zywl.service.UnitService;
import com.zywl.service.UserService;
import com.zywl.utils.models.AjaxResult;
import com.zywl.utils.utils.FileUploadUtil;
import com.zywl.utils.utils.FileUtil;
import com.zywl.utils.utils.GenerateCodeUtil;
import com.zywl.utils.utils.IPUtil;
import com.zywl.web.controller.base.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: xushixiang
 * @Date: 2022-04-18
 * @Description:
 */
@RestController
@RequestMapping("/")
public class UtilsController extends BaseController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Autowired
    private UnitService unitService;

    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(HttpServletRequest request, MultipartFile file, String folderName, boolean isAppendIp) {
        AjaxResult ar = new AjaxResult();
        String filePath = null;
        try {
            filePath = FileUploadUtil.fileUpload(request, file, folderName);
            if (isAppendIp) {
                filePath = IPUtil.getIpPath(request) + filePath;
            }
            ar.success("上传成功", filePath);
        } catch (Exception e) {
            logger.error("上传文件异常", e);
            ar.error("上传文件异常", e);
        }
        return ar;
    }

    @PostMapping("/deleteFile")
    public AjaxResult deleteFile(HttpServletRequest request, String filePath) {
        AjaxResult ar = new AjaxResult();
        try {
            FileUtil.deleteFileForWeb(request, filePath);
            ar.success("删除文件成功");
        } catch (Exception e) {
            ar.error("删除文件异常", e);
        }
        return ar;
    }

    @PostMapping("/isExist")
    public boolean isExist(String value, String type) {
        try {
            if (StringUtils.isNotEmpty(value)) {
                if ("roleNameExist".equals(type)) {
                    return roleService.roleNameExist(value);
                } else if ("userUsernameExist".equals(type)) {
                    if (userService.getUsersByUsername(value) != null) {
                        return true;
                    }
                } else if ("unitNoExist".equals(type)) {
                    if (unitService.getUnitByUnitNo(value) != null) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            logger.error("判断" + type + "是否存在异常", e);
        } finally {
            value = null;
            type = null;
        }
        return false;
    }

    @PostMapping("/isOldPassword")
    public boolean isOldPassword(String id, String password) {
        if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(password)) {
            try {
                User user = userService.getById(Long.parseLong(id));
                if (user != null && GenerateCodeUtil.MD5(password).equals(user.getUsrPassword())) {
                    return true;
                }
            } catch (Exception e) {
                logger.error("获取人员异常", e);
            }
        }
        return false;
    }
}
